home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / palnfilt.c < prev    next >
C/C++ Source or Header  |  1995-03-14  |  3KB  |  121 lines

  1. /*
  2. **  PALNFILT.C - A palindrome filter.
  3. **
  4. **  Reads lines of text and only passes lines which are palindromes, i.e.
  5. **  those which read the same forwards or backwards. Includes options switches
  6. **  to ignore case and/or punctuation, e.g.
  7. **
  8. **  Input               Output      w/ -C       w/ -P       w/ -C -P
  9. **  ---------------     ----------  ----------  ----------  ---------------
  10. **  toot                toot        toot        toot        toot
  11. **  Toot                            Toot                    Toot
  12. **  toot!                                       toot!       toot!
  13. **  Madam, I'm Adam                                         Madam, I'm Adam
  14. **  This is a test.
  15. **  -----------------------------------------------------------------------
  16. **
  17. **  public domain demo by Bob Stout
  18. **
  19. **  Uses GETOPTS.C and CANT.C, also from SNIPPETS
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "getopts.h"
  26.  
  27. #define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
  28. #define NUL '\0'
  29.  
  30. FILE *cant(char *fname, char *fmode);     /* From CANT.C in SNIPPETS    */
  31. void usage(int exitval);
  32. char *rmpunc(char *str);
  33.  
  34. Boolean_T fold = FALSE, punc = FALSE, help = FALSE;
  35.  
  36. struct Option_Tag options[] = {
  37.       {'c', Boolean_Tag, &fold},
  38.       {'C', Boolean_Tag, &fold},
  39.       {'p', Boolean_Tag, &punc},
  40.       {'P', Boolean_Tag, &punc},
  41.       {'h', Boolean_Tag, &help},
  42.       {'H', Boolean_Tag, &help},
  43.       { 0 , ERROR      , NULL }
  44. };
  45.  
  46. /*
  47. **  Implemented as a true filer, defaulting to stdin and stdout.
  48. */
  49.  
  50. main(int argc, char *argv[])
  51. {
  52.       FILE *infile = stdin, *outfile = stdout;
  53.       char line[2][256];                        /* Nice & roomy   */
  54.  
  55.       if (ERROR == getopts(argc, argv))
  56.             usage(-1);
  57.       if (help)
  58.             usage(0);
  59.       if (1 < xargc)
  60.             infile = cant(xargv[1], "r");
  61.       if (2 < xargc)
  62.             outfile = cant(xargv[2], "w");
  63.       while (NULL != fgets(line[0], 255, infile))
  64.       {
  65.             char *p1, *p2;
  66.             int OK;
  67.  
  68.             strcpy(line[1], line[0]);
  69.             if ('\n' == LAST_CHAR(line[1]))
  70.                   LAST_CHAR(line[1]) = NUL;
  71.             if (fold)
  72.                   strupr(line[1]);
  73.             if (punc)
  74.                   rmpunc(line[1]);
  75.             for (p1 = line[1], p2 = &LAST_CHAR(line[1]), OK = 1;
  76.                   p2 > p1; ++p1, --p2)
  77.             {
  78.                   if (*p1 != *p2)
  79.                   {
  80.                         OK = 0;
  81.                         break;
  82.                   }
  83.             }
  84.             if (OK)
  85.                   fputs(line[0], outfile);
  86.       }
  87. }
  88.  
  89. /*
  90. **  Tell 'em how it works
  91. */
  92.  
  93. void usage(int exitval)
  94. {
  95.       puts("Usage: PALNFILT [-cph] [infile] [outfile]");
  96.       puts("where: h = Provide help (this display)");
  97.       puts("       c = Ignore case");
  98.       puts("       p = Ignore punctuation");
  99.       puts("       infile defaults to stdin");
  100.       puts("       outfile defaults to stdout");
  101.       puts("notes: switches may not be concatenated but may be any case");
  102.       exit(exitval);
  103. }
  104.  
  105. /*
  106. **  Remove all punctuation from a a string
  107. */
  108.  
  109. char *rmpunc(char *str)
  110. {
  111.       char *obuf, *nbuf;
  112.  
  113.       for (obuf = str, nbuf = str; *obuf && obuf; ++obuf)
  114.       {
  115.             if (isalpha(*obuf))
  116.                   *nbuf++ = *obuf;
  117.       }
  118.       *nbuf = NUL;
  119.       return str;
  120. }
  121.